
$().ready(function() {
	validateRule();
});
$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

function save() {
	var name = $.trim($('#name').val());
	var centreId = $.trim($('#centreId').val());
	$.post("/warehouse/manage/isNameRepeat", {"name":name, "centreId":centreId},function(data){
		var obj = eval(data);
		if(obj == 'Y'){
			layer.msg("仓库名称重复");
			return;
		}else{
			$.ajax({
				cache : true,
				type : "POST",
				url : "/warehouse/manage/save",
				data : $('#signupForm').serialize(),// 你的formid
				async : false,
				error : function(request) {
					parent.layer.alert("Connection error");
				},
				success : function(data) {
					if (data.code == 0) {
						parent.layer.msg("操作成功");
						parent.reLoad();
						var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
						parent.layer.close(index);
					} else {
						if (data.msg) {
							parent.layer.alert(data.msg)
						}else {
							window.location.href = "/";
						}
					}

				}
			});
		}
	});
}



function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			code : {
				required : true
			},
			centreId : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "仓库名称不能为空"
			},
			code : {
				required : icon + "仓库编码不能为空"
			},
			centreId : {
				required : icon + "请选择商户"
			}
		}
	})
}

